package com.xiaoyg.direct;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Random;

/**
 * @author xiaoyg
 * @Title: ReveiveLogsDirect
 * @ProjectName rabbitmq-direct
 * @Description: TODO
 * @date 2018/12/416:56
 */


//接收端随机设置一个日志严重级别（binding_key）。。
public class ReveiveLogsDirect {

    //交换名称
    private static final String EXCHANGE_NAME = "exchange_logs_direct";

    //日志分类

    private static final String [] SEVERITIES = {"info","warning","error"};

    public static void main(String[] args) throws IOException, InterruptedException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.8.129");
        connectionFactory.setPort(AMQP.PROTOCOL.PORT);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();


        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        String queueName = channel.queueDeclare().getQueue();

        String str = getStr();

        channel.queueBind(queueName,EXCHANGE_NAME,str);

        System.out.println(" [*] Waiting for "+str+" logs. To exit press CTRL+C");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName,true,consumer);

        while (true){
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message  = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
        }
    }

    private static String getStr() {
        Random random = new Random();
//        int value = random.nextInt(3);
        return SEVERITIES[1];
    }


}
